package com.example.demo8;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img;      //封面图
    private static SeekBar seekBar;       //进度条
    private static TextView start_Time,end_time;      //歌曲开始时间和总长
    private Button btn_start,btn_pause,btn_continue,btn_quit;       //四个控制按钮
    private ObjectAnimator animator;    //定义一个动画组件

    private MusicService.MusicBinder  musicControl;     //服务控制器binder实例化
    private MyServiceConn myServiceconn;    //连接实例化
    private Intent intent;      //全局的意图对象
    private boolean isUnbind;     //记录服务是否被解绑

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFunction();
    }
    /*
    初始化组件
     */
    private void initView() {
        img=findViewById(R.id.img);
        seekBar=findViewById(R.id.seek_bar);
        start_Time=findViewById(R.id.start_Time);
        end_time=findViewById(R.id.end_time);

        btn_start=findViewById(R.id.btn_start);
        btn_pause=findViewById(R.id.btn_pause);
        btn_continue=findViewById(R.id.btn_continue);
        btn_quit=findViewById(R.id.btn_quit);

        btn_start.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_continue.setOnClickListener(this);
        btn_quit.setOnClickListener(this);
    }
    /*
    初始化组件功能
     */
    private void initFunction()
    {
        //把图片设置为旋转0到360度动画
        animator=ObjectAnimator.ofFloat(img,"rotation",0.0f,360.0f);
        animator.setDuration(10000);    //旋转一周用的时长
        animator.setInterpolator(new LinearInterpolator()); //设置匀速旋转
        animator.setRepeatCount(-1);    //-1是设置无限循环

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //进度变化的时候的处理
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //滑动到最大时，结束动画
                if (i==seekBar.getMax())
                {
                    animator.pause();       //停止动画播放
                }
            }
            //进度滑动的时候的处理
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            //停止滑动时的处理
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();   //获取seekBar的进度值
                //调用服务中的seeTo方法改进音乐的进度
                musicControl.seekTo(progress);
            }
        });
        //打开服务的意图
        intent =new Intent(this,MusicService.class);
        myServiceconn=new MyServiceConn();  //实例化服务对象
        bindService(intent,myServiceconn,BIND_AUTO_CREATE);     //绑定服务
    }
    /*
    创建一个主线程处理对象
     */
    public static Handler handler=new Handler(){
        //处理子线程传来的消息
        @Override
        public void handleMessage(@NonNull Message msg) {
            //super.handleMessage(msg);
            Bundle bundle=msg.getData();        //获取信息
            int leng=bundle.getInt("leng");     //总时长
            int currentPosition=bundle.getInt("currentPosition");   //当前播放时长

            seekBar.setMax(leng);      //设置真实播放总时长
            seekBar.setProgress(currentPosition);       //设置当前播放进度

            /*
                显示总时长开始
             */
            int minute=leng/1000/60;      //分钟计算
            int second=leng/1000%60;      //秒数计算
            //设置显示
            String strminute="";
            String strsecond="";
            if (minute<10)
            {
                strminute="0"+minute;
            }else
            {
                strminute=minute+"";
            }
            if (second<10)
            {
                strsecond="0"+second;
            }else
            {
                strsecond=second+"";
            }
            end_time.setText(strminute+":"+strsecond);
            /*
                显示总时长结束
             */
            /*
                显示播放时长开始
             */
             minute=currentPosition/1000/60;      //分钟计算
             second=currentPosition/1000%60;      //秒数计算
            //设置显示
            if (minute<10)
            {
                strminute="0"+minute;
            }else
            {
                strminute=minute+"";
            }
            if (second<10)
            {
                strsecond="0"+second;
            }else
            {
                strsecond=second+"";
            }
            start_Time.setText(strminute+":"+strsecond);
            /*
                显示播放时长结束
             */
        }
    };



    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btn_start:        //开始播放
                musicControl.play();
                animator.start();
                break;
            case R.id.btn_pause:        //暂停播放
                musicControl.pausePlay();
                animator.pause();
                break;
            case R.id.btn_continue:     //继续播放
                musicControl.continueplay();
                animator.start();
                break;
            case R.id.btn_quit:         //退出
                myUnbind();     //解除绑定
                finish();       //关闭界面
                break;
        }
    }
    //自定义解绑方法
    private void myUnbind()
    {
        if (!isUnbind)
        {
            isUnbind=true;
            musicControl.pausePlay();       //暂停播放
            unbindService(myServiceconn);   //解绑服务
            stopService(intent);        //停止服务
        }
    }

    //用于连接服务的自定义类
    class MyServiceConn implements ServiceConnection
    {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            musicControl=(MusicService.MusicBinder)iBinder;

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}